<?php
require_once 'SimpleImage.php';
require_once 'S3.php';

class file_uploader_server {

    public $config = array();
    public $bucketName = "slopetrackereastercalendar"; //slopetrackereastercalendar

    /**
     *
     * @var type S3
     */
    public $s3 = null;

    public function __construct($config) {
        $this->config = $config;
        $this->s3 = new S3('AKIAIHDJ6K7LJJJAU3VQ', 'xgjH7Ug3raDBxtfOXDphvjee1zmqBD+v8RguH5W5');
    }

    public function uploadToAmazonS3($imgFileLocation, $imgFileName) {        
        $return = $this->s3->putObjectFile($imgFileLocation, $this->bucketName, "users/{$this->config['user_id']}/" . $imgFileName, S3::ACL_PUBLIC_READ);
        if ($return) {
            $data = array(
                'url' => "http://" . $this->bucketName . ".s3.amazonaws.com/users/{$this->config['user_id']}/" . $imgFileName
            );
            return $data['url'];
        }
        return false;
    }
    
    public function deleteFromAmazonS3($imgFileName) {
        $this->s3->deleteObject($this->bucketName, "users/{$this->config['user_id']}/" . $imgFileName);
    }
    
    public function deleteImgFile($fileLocation) {
        if (substr($fileLocation, 0, 4) == "http") {
            $this->deleteFromAmazonS3(basename($fileLocation));
        } else {
            @unlink("../" . $fileLocation);
        }
    }
    
    public function handledeletefile($fileKey, $oldFile) {

        if (isset($_POST['deleteimg'][$fileKey]) && $oldFile) {
            // process delete file only
            if (substr($oldFile, 0, 4) == "http") {
                $this->deleteFromAmazonS3(basename($oldFile));
            } else {
                @unlink("../" . $oldFile);
            }
            return true;
        }
        return false;
    }

    public function handleuploadfile($submitFile, $maxSizeWidth = false, $oldFile = false) {
        $result = array(
            'fileurl' => false
        );
        if ($submitFile['error']) {
            return $result;
        }
        $image = new SimpleImage();
        $fileName = time() . "_" . basename($submitFile['name']);
        $target_path = "../uploads/";
        $target_path = $target_path . $fileName;
        $savePath = "uploads/" . $fileName;
        if (move_uploaded_file($submitFile['tmp_name'], $target_path)) {

            //resize new file
            if ($maxSizeWidth) {
                // resize and override image                        
                $image->load($target_path);
                $w = $image->getWidth();
                $h = $image->getHeight();                
                if ($w > $maxSizeWidth) {
                    $newH = ceil($maxSizeWidth * $h / $w);
                    $image->resize($maxSizeWidth, $newH);
                    $image->save($target_path);
                }
            }

            // delete old file
            if ($oldFile) {
                if (substr($oldFile, 0, 4) == "http") {
                    $this->deleteFromAmazonS3(basename($oldFile));
                } else {
                    @unlink("../" . $oldFile);
                }
            }
            $result['fileurl'] = $savePath;

            // upload to manazon S3
            $s3file = $this->uploadToAmazonS3($target_path, $fileName);
            if ($s3file) {
                // delete the old one
                @unlink("../" . $savePath);
                $result['fileurl'] = $s3file;
            }
        }
        return $result;
    }

}
